﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using weikebuluo.DAL;
using weikebuluo.Model;
using weikebuluo.Common;

namespace weikebuluo.wxgzpt.Customer.GuestBook
{
    public partial class BlackList : weikeweixin.web.admin.cs.adminbase
    {
        C_BlackList_DAL cBlackListDAL = new C_BlackList_DAL();
        JsonOperate JsonOperate = new JsonOperate();

        C_Guestbook_DAL cGuestbookDAL = new C_Guestbook_DAL();
        Model.C_Guestbook_Model GuestbookModel = new Model.C_Guestbook_Model();

        protected void Page_Load(object sender, EventArgs e)
        {
            switch (Request.QueryString["action"])
            {
                case "bindBlackList":
                    GetBlackList();
                    break;
                case "del":
                    DelBlack();
                    break;
            }

        }

        /// <summary>
        ///查询留言的会员列表
        /// </summary>
        public void GetBlackList()
        {
            string where = "User_ID=" + user_id;
            int Page = int.Parse(Request.Form["page"]);
            int RecordCount = cBlackListDAL.GetRecordCount(where);

            DataTable dtBlackList = cBlackListDAL.GetListByPage(where, "BlackList_ID", ((Page - 1) * 15) + 1, (Page - 1) * 15 + 15).Tables[0];
            Response.Write(JsonOperate.GetDataGridJson(dtBlackList, RecordCount));
            Response.End();
        }

        /// <summary>
        /// 解除加黑的会员
        /// </summary>
        public void DelBlack() 
        {
            if (!Request.QueryString["Id"].Trim().IsNullOrEmpty())
            {
                int BlackList_ID = int.Parse(Request.QueryString["Id"].Trim());
                DataTable dtBlackList = cBlackListDAL.GetList("BlackList_ID =" + BlackList_ID).Tables[0];
                string BlackList_WeChatID = dtBlackList.Rows[0]["BlackList_WeChatID"].ToString();
                int count = cGuestbookDAL.GetRecordCount("Guestbook_WeChatID = '" + BlackList_WeChatID + "'");

                DataTable dtGuestbook;

                //解除黑名单的会员
                for (int i = 0; i < count; i++)
                {
                    dtGuestbook = cGuestbookDAL.GetList("Guestbook_WeChatID = '" + dtBlackList.Rows[0]["BlackList_WeChatID"] + "'").Tables[0];
                    GuestbookModel = cGuestbookDAL.GetModel(int.Parse(dtGuestbook.Rows[i]["Guestbook_ID"].ToString()));
                    GuestbookModel.Guestbook_Stutas = 2;
                    cGuestbookDAL.Update(GuestbookModel);
                }
                //删除
                cBlackListDAL.Delete(int.Parse(Request.QueryString["Id"].Trim()));
            }
        }
    }
}
